//! 消息类型
//!

use serde_repr::{Deserialize_repr, Serialize_repr};
use std::fmt::Display;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize_repr, Deserialize_repr)]
#[repr(u8)]
pub enum MessageType {
    #[default]
    Transactions = 1, // 交易消息
    Contract = 2, //  合同消息
    SnapShot = 3, // 快照
    Text = 4,     // 文件消息
}

impl From<u8> for MessageType {
    fn from(value: u8) -> Self {
        match value {
            1 => Self::Transactions,
            2 => Self::Contract,
            3 => Self::SnapShot,
            4 => Self::Text,
            _ => panic!("unsupported message type"),
        }
    }
}

impl Into<u8> for MessageType {
    fn into(self) -> u8 {
        self as u8
    }
}

impl Display for MessageType {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            MessageType::Transactions => f.write_str("Transactions"),
            MessageType::Contract => f.write_str("Contract"),
            MessageType::SnapShot => f.write_str("SnapShot"),
            MessageType::Text => f.write_str("Text"),
        }
    }
}
